var fs = require('fs')
var path = require('path')
var Q = require('q')

var fs_readdir = Q.denodeify(fs.readdir);
var fs_stat = Q.denodeify(fs.stat);

module.exports = function(dir) {
	return fs_readdir(dir).then(function(files) {
		var promises = files.map(function(file) {
			return fs_stat(path.join(dir, file));
		})

		return Q.all(promises).then(function(stats) {
			return [files, stats];
		});
	}).then(function(data) {
		var files = data[0];
		var stats = data[1];
		var largest = stats.filter(function(stat) {
				return stat.isFile();
			})
			.reduce(function(pre, next) {
				return pre.size > next.size ? pre : next;
			});

		return files[stats.indexOf(largest)];
	});
}